const puppeteer = require('puppeteer')
const { COOKIE } = require('./config')

const cookies = {
    name: 'wt2',
    value: COOKIE,
    domain: 'www.zhipin.com'
}

;(async () => {
    const browser = await puppeteer.launch({
        headless: false,
    })

    const page = await browser.newPage()
    
    await page.setCookie(cookies)

    await page.goto('https://www.zhipin.com/web/geek/job?query=%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88&city=101280700&page=1')

    await waitTime(4500)

    const list = await page.$$eval('.search-job-result .job-list-box .job-card-wrapper', els => {
        let arr = []

        // 过滤已沟通过的
        arr = els.filter(el => {
            return el.querySelector('.start-chat-btn').innerHTML === '立即沟通'
        })

        // 提取信息
        arr = arr.map(el => {
            return {
                href: el.querySelector('.job-card-body > a').href,
                companyName: el.querySelector('.company-name a').innerHTML,
                jobName: el.querySelector('.job-title .job-name').innerHTML,
                salary: el.querySelector('.job-info .salary').innerHTML,
            }
        })
        return arr
    })
    console.log(list, 'list')
})();


const waitTime = (time = 1000) => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, time);
    })
}

class Reptile {
    constructor(cookies){
        this.cookies = cookies
    }
    async open(){
        this.browser = await puppeteer.launch({
            headless: false,
        })
        await page.setCookie(cookies)
    }
}